package com.hualan.dao.impl;

import com.hualan.bean.PanghuShop;
import com.hualan.dao.StoreDao;
import com.hualan.utils.DruidUtil;
import java.sql.SQLException;

import java.util.ArrayList;


import java.util.List;
import java.util.Map;

public class StoreDaoImpl implements StoreDao {
    //新增,插入数据
    @Override
    public int insert(PanghuShop entity) {


        int row = DruidUtil.update("insert into panghu_shop (shop_name, shop_describe, shop_image, shop_phone, shop_province, shop_city, shop_district, shop_address, shop_longitude, shop_latitude) values (?,?,?,?,?,?,?,?,?,?)",
                entity.getShopName(),
                entity.getShopDescribe(),
                entity.getShopImage(),
                entity.getShopPhone(),
                entity.getShopProvince(),
                entity.getShopCity(),
                entity.getShopDistrict(),
                entity.getShopAddress(),
                entity.getShopLongitude(),
                entity.getShopLatitude());

        return row;
    }

    //删除数据
    @Override
    public int delete(Integer id) throws SQLException {

        int row = DruidUtil.update("delete from panghu_shop");


        return row;
    }

    //编辑,修改数据
    @Override
    public int update(PanghuShop entity) {

        int row = DruidUtil.update("update panghu_shop set shop_id = ?,shop_name = ?,shop_describe = ?,shop_image = ?,shop_phone = ?,shop_province = ?,shop_city = ?,shop_district = ?,shop_address = ?,shop_longitude = ?,shop_latitude = ? where shop_id = ?",entity.getShopName(),entity.getShopDescribe(),entity.getShopImage(),entity.getShopPhone(),entity.getShopProvince(),entity.getShopCity(),entity.getShopDistrict(),entity.getShopAddress(),entity.getShopLongitude(),entity.getShopLatitude(),entity.getShopId());

//        System.out.println("dao层里面的id:" + entity.getShopId());

        return row;
    }

    //按id查询
    @Override
    public PanghuShop selectById(Integer id) {
        PanghuShop panghuShop = DruidUtil.queryOne("select * from panghu_shop where shop_id = ?", PanghuShop.class, id);
        return panghuShop;
    }

    //分页查询
    @Override
    public List<PanghuShop> selectByPage(Integer pageNum, Integer pageSize) {
        List<PanghuShop> shopList = DruidUtil.query("select * from panghu_shop limit ?,?",PanghuShop.class,(pageNum-1) * pageSize,pageSize);

        return shopList;
    }

    //查询所有
    @Override
    public long selectAll() {
        Map<String, Object> map = DruidUtil.queryOne("select count(*) as count from panghu_shop");
        Long count = (Long) map.get("count");
        return count;
    }

    public List<PanghuShop> panghuShopList(String pageNum, String pageSize, String shopName,String shopPhone, String startTime, String endTime) {
        // 判断查询条件
        String condition = " where 1 = 1";
        // 用列表接收有效参数
        List<String> params = new ArrayList<>();
        if (shopName != null && !"".equals(shopName)){
            condition += " and shop_name like ?";
            params.add("%" + shopName + "%");
        }
        if (shopPhone != null && !"".equals(shopPhone)){
            condition += " and shop_phone like ?";
            params.add("%" + shopPhone + "%");
        }
        if (startTime != null && !"".equals(startTime)){
            condition += " and add_time >= ?";
            params.add(startTime);
        }
        if (endTime != null && !"".equals(endTime)){
            condition += " and add_time <= ?";
            params.add(endTime);
        }
        condition += " limit " + pageNum + ", " + pageSize;
        String sql = "SELECT shop_name,shop_id,shop_describe, shop_image, shop_phone, shop_province, shop_city, shop_district, shop_address, shop_longitude, shop_latitude FROM panghu_shop  " + condition;
        System.out.println("完整的查询条件" + sql);

        System.out.println("查询前：" + params);
        // params.toArray(new Object[0])将列表转化成长度为0的object类型数组
        List<PanghuShop> panghuShopList = DruidUtil.query(sql, PanghuShop.class, params.toArray());

        System.out.println("本次查询到的总条数：" +panghuShopList.size());
        return panghuShopList;

    }


}

